package com.itheima.sc.impl.pms;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sc.api.pms.BrandApi;
import com.itheima.sc.mapper.pms.BrandMapper;
import com.itheima.sc.pojo.pms.Brand;
import com.itheima.sc.vo.BrandVo;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class BrandImpl extends ServiceImpl<BrandMapper, Brand> implements BrandApi {

    @Override    //根据品牌名称分页获取品牌列表
    public List<BrandVo> listByPage(String keyword, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(keyword),Brand::getName,keyword);
        wrapper.orderByAsc(Brand::getName); //按照品牌名称排序

        Page<Brand> pageInfo = new Page<>(pageNum, pageSize);
        pageInfo = page(pageInfo, wrapper);
        List<Brand> brandList = pageInfo.getRecords();

        ArrayList<BrandVo> brandVos = new ArrayList<>();
        for (Brand brand : brandList) {
            BrandVo brandVo = new BrandVo();
            BeanUtil.copyProperties(brand,brandVo);
            brandVo.setId(Convert.toInt(brand.getId()));
            brandVos.add(brandVo);
        }

        return brandVos;
    }

    @Override  //批量更新厂家制造商状态
    public void updateFactoryStatus(Integer factoryStatus, List<Integer> ids) {
        List<Brand> brandList = listByIds(ids);
        for (Brand brand : brandList) {
            brand.setFactoryStatus(factoryStatus);
            updateById(brand);
        }
    }

    @Override     //批量更新显示状态
    public void updateShowStatus(Integer showStatus, List<Integer> ids) {
        List<Brand> brandList = listByIds(ids);
        for (Brand brand : brandList) {
            brand.setShowStatus(showStatus);
            updateById(brand);
        }
    }

    @Override   //根据品牌名称查询totalCount
    public Integer getcount(String keyword) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(keyword),Brand::getName,keyword);

        int count = count(wrapper);
        return count;
    }

    @Override   //更新brand表中productCount产品数量+1
    public void updateProductCount(Long brandId) {
        Brand brand = getById(brandId);
        Integer productCount = brand.getProductCount();
        if (productCount==null){
            brand.setProductCount(1);
        }else {
            brand.setProductCount(productCount+1);
        }
        updateById(brand);
    }

    @Override  //先根据pid删除brand表中brand对应productCount产品数量-1
    public void removeProductCount(Long brandId) {
        Brand brand = getById(brandId);
        Integer productCount = brand.getProductCount();
        //取出旧的产品数量并删除
        brand.setProductCount(productCount-1);
        updateById(brand);
    }


}
